cargo-run: support target and release the same as cargo-build
authorCorey Richardson <corey@octayn.net>
Mon, 25 Aug 2014 14:51:13 +0000 (10:51 -0400)
committerCorey Richardson <corey@octayn.net>
Mon, 25 Aug 2014 15:49:54 +0000 (11:49 -0400)
src/bin/cargo-run.rs
src/cargo/ops/cargo_run.rs
tests/support/mod.rs
tests/test_cargo_cross_compile.rs
tests/test_cargo_run.rs

index 510027cdaba57845d1d394681ced04e3c39a45c6..c945d9b8204b89894a675cde34a40bbb62f4b2e5 100644 (file)
@@ -22,6 +22,8 @@ Usage:
 Options:
     -h, --help              Print this message
     -j N, --jobs N          The number of jobs to run in parallel
+    --release               Build artifacts in release mode, with optimizations
+    --target TRIPLE         Build for the target triple
     -u, --update-remotes    Deprecated option, use `cargo update` instead
     --manifest-path PATH    Path to the manifest to execute
     -v, --verbose           Use verbose output
@@ -40,10 +42,10 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
 
     let mut compile_opts = ops::CompileOptions {
         update: options.flag_update_remotes,
-        env: "compile",
+        env: if options.flag_release { "release" } else { "compile" },
         shell: shell,
         jobs: options.flag_jobs,
-        target: None,
+        target: options.flag_target.as_ref().map(|t| t.as_slice()),
         dev_deps: true,
     };
 
index 0862f27322cb73cf628302173d7a300560a9330b..ae5e83ce3cfc1000617ff15207af4a1750e44050 100644 (file)
@@ -17,7 +17,11 @@ pub fn run(manifest_path: &Path,
     let root = try!(src.get_root_package());
 
     let compile = try!(ops::compile(manifest_path, options));
-    let exe = manifest_path.dir_path().join("target").join(root.get_name());
+    let mut exe = manifest_path.dir_path().join("target");
+    if options.env == "release" {
+        exe = exe.join("release");
+    }
+    let exe = exe.join(root.get_name());
     let exe = match exe.path_relative_from(&os::getcwd()) {
         Some(path) => path,
         None => exe,
index 24442162ae410f3c8a822dc1e5f98f5dcd23ee2e..048e2091ce71f587de2b9a3a6240cf233ff792dd 100644 (file)
@@ -102,6 +102,10 @@ impl ProjectBuilder {
         self.build_dir().join(format!("{}{}", b, os::consts::EXE_SUFFIX))
     }
 
+    pub fn release_bin(&self, b: &str) -> Path {
+        self.build_dir().join("release").join(format!("{}{}", b, os::consts::EXE_SUFFIX))
+    }
+
     pub fn target_bin(&self, target: &str, b: &str) -> Path {
         self.build_dir().join(target).join(format!("{}{}", b,
                                                    os::consts::EXE_SUFFIX))
index b463fd4f707cde1510a968dcccf26db76f3edc09..d122408f40c7948ab79e4b9da5ee7c0628ed6eea 100644 (file)
@@ -434,3 +434,43 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
 ", compiling = COMPILING, running = RUNNING, foo = p.url(), triple = target,
    doctest = DOCTEST)));
 })
+
+test!(simple_cargo_run {
+    if disabled() { return }
+
+    let target = alternate();
+
+    let build = project("builder")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.5.0"
+            authors = ["wycats@example.com"]
+        "#)
+        .file("src/main.rs", format!(r#"
+            fn main() {{
+                assert_eq!(std::os::getenv("TARGET").unwrap().as_slice(), "{}");
+            }}
+        "#, target).as_slice());
+    assert_that(build.cargo_process("cargo-run").arg("--target").arg(target),
+                execs().with_status(0));
+
+    let p = project("foo")
+        .file("Cargo.toml", format!(r#"
+            [package]
+            name = "foo"
+            version = "0.0.0"
+            authors = []
+            build = '{}'
+        "#, build.bin("foo").display()))
+        .file("src/main.rs", r#"
+            use std::os;
+            fn main() {
+                assert_eq!(os::consts::ARCH, "x86");
+            }
+        "#);
+
+    let target = alternate();
+    assert_that(p.cargo_process("cargo-run").arg("--target").arg(target),
+                execs().with_status(0));
+})
index adc4f0c6ebb810cfb85e8996ab35effcb164d8e8..860a62e51f5948b69a87c37376a98a541a196398 100644 (file)
@@ -113,3 +113,27 @@ test!(run_dylib_dep {
     assert_that(p.cargo_process("cargo-run").arg("hello").arg("world"),
                 execs().with_status(0));
 })
+
+test!(release_works {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/main.rs", r#"
+            fn main() { if !cfg!(ndebug) { fail!() } }
+        "#);
+
+    assert_that(p.cargo_process("cargo-run").arg("--release"),
+                execs().with_status(0).with_stdout(format!("\
+{compiling} foo v0.0.1 ({dir})
+{running} `target{sep}release{sep}foo`
+",
+        compiling = COMPILING,
+        running = RUNNING,
+        dir = path2url(p.root()),
+        sep = path::SEP).as_slice()));
+    assert_that(&p.release_bin("foo"), existing_file());
+})